<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;
use think\facade\Event;

class Account extends Base
{
    protected $name = 'user_account';

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function take(): object
    {
        return $this->hasOne(User::class, 'id', 'source')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function getTypeAttr($value): string
    {
        return get_pay_way($value);
    }

    /**
     * 奖金入账
     */
    public static function bonusEntry($uid, $amount, $intro, $source, $category): bool|string
    {
        try {
            if ($amount > 0 && !empty($uid)) {
                Target::Account()->basics($uid, 'bonus', $amount, $intro, '+')
                    ->createBonus($source, $category);
                Event::trigger('Statistics', ['key' => 'expend', 'value' => $amount]);
                $content = "您获得【{$intro}】【{$amount}】元，已发放至您的账户余额，请注意查收。";
                UserNotify::add(1, $uid, $intro . '-收益到账', $content);
            }

            return true;
        } catch (\Exception $e) {

            return $e->getMessage();
        }
    }
}